import { useRequest } from "ahooks";
import { rcRequest } from "@core/rc-components/es/RcRequest";
import { useState } from "react";
export interface IApproveTimeLineProps {
  busId?: string;
  headers?: any;
  itemClickHandle?: any;
  data?: IApproveTimeLineQueryData[];
  url?: string;
  titleStart?: string;
  orderType?: number;
}
export interface IApproveTimeLineQueryData {
  id: string;
  busType: number;
  orderTime: number;
  approveState: number;
  orderStatus: number;
  flowUnits: any[];
  procInstId?: string;
}
export const useApproveTimeLineQuery = ({
  busId,
  headers = {},
  url = "/api/scm/v1/common/cancel/approve/query",
  orderType = 6,
}: IApproveTimeLineProps) => {
  const [lastItem, setLastItem] = useState<IApproveTimeLineQueryData | null>(
    null
  );
  const { data, refresh, loading } = useRequest(
    () =>
      rcRequest<IApproveTimeLineQueryData[]>(url, {
        data: {
          busId: busId,
          filterFlag: 1,
          orderType,
        },
        method: "post",
        headers: {
          ...headers,
        },
      }),
    {
      refreshDeps: [busId],
      ready: !!busId,
      onSuccess: (approveTimeLineData) => {
        const lastItem = approveTimeLineData[approveTimeLineData.length - 1];
        if (lastItem) {
          setLastItem(lastItem);
        }
      },
    }
  );

  return {
    data,
    refresh,
    loading,
    lastItem,
    setLastItem,
  };
};
